package com.bw.zzy.partal2.service.impl;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.MD5;
import com.bw.zzy.partal2.db.dao.UserDao;
import com.bw.zzy.partal2.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Set;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserDao userDao;

    @Override
    public Integer userLogin(HashMap map) {
        String username = MapUtil.getStr(map, "username");
        String password = MapUtil.getStr(map, "username");
        MD5 md5 = MD5.create();
        String temp = md5.digestHex(username);        //对用户名进行MD5加密
        String tempStart = StrUtil.subWithLength(temp, 0, 6);//截取MD5加密后的前3位作为临时字符串的开头
        String tempEnd = StrUtil.subSuf(temp, temp.length() - 3);  //截取MD5加密后的3位作为临时字符串的结尾
        password = md5.digestHex(tempStart + password + tempEnd);   //将密码与临时字符串拼接后再进行MD5加密


        map.replace("password", password);   //替换map中的密码值为加密后的密码
        return userDao.login(map);  //调用userDao的login方法进行用户登录
    }

    @Override
    public Set<String> selectUserPrivileges(int userId) {
        return userDao.selectUserPrivileges(userId);
    }

}
